package com.shenyue.nbtownlet.fragment;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.shenyue.nbtownlet.R;
import com.shenyue.nbtownlet.adapter.OrganizationIntroduceListViewAdapter;
import com.shenyue.nbtownlet.base.BaseFragment;
import com.shenyue.nbtownlet.databinding.OrganizationIntroduceFragmentBinding;
import com.shenyue.nbtownlet.event.BaseListener;
import com.shenyue.nbtownlet.model.AppContactsModel;
import com.shenyue.nbtownlet.model.AppLoginModel;
import com.shenyue.nbtownlet.util.NetWorkUtils;
import com.shenyue.nbtownlet.util.StringUtil;

/**
 * 机构介绍
 */

public class OrganizationIntroduceFragment extends BaseFragment {
    private OrganizationIntroduceFragmentBinding binding;
    private Context mContext;
    private boolean isLoading = false;
    private OrganizationIntroduceListViewAdapter listViewAdapter;
    private Handler mHandler = new Handler();
    private int pageNo = 1, pageSize = 10;

    public static BaseFragment newInstance(String name) {
        Bundle args = new Bundle();
        args.putString("name", name);
        BaseFragment fragment = new OrganizationIntroduceFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.organization_introduce_fragment, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mContext = getActivity();

        listViewAdapter = new OrganizationIntroduceListViewAdapter(mContext);
        binding.listView.setAdapter(listViewAdapter);

        binding.ptrClassicFrameLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                if (!NetWorkUtils.isNetworkConnected(mContext)) {
                    binding.ptrClassicFrameLayout.refreshComplete();
                    showMsg(getString(R.string.no_network));
                    return;
                }
                query();
            }
        });

        binding.ptrClassicFrameLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                if (!NetWorkUtils.isNetworkConnected(mContext)) {
                    binding.ptrClassicFrameLayout.loadMoreComplete(false);
                    showMsg(getString(R.string.no_network));
                    return;
                }
                queryMore();
            }
        });
        binding.ptrClassicFrameLayout.disableWhenHorizontalMove(true);
    }

    @Override
    public void setLoading(boolean loading) {
        isLoading = loading;
    }

    @Override
    public void refresh(JSONObject data) {
        if (isLoading)
            return;
        isLoading = true;

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                binding.ptrClassicFrameLayout.autoRefresh();
            }
        }, 32);
    }

    @Override
    public void willBeDisplayed() {

    }

    private void query() {
        listViewAdapter.clear();
        pageNo = 1;
        JSONObject param = new JSONObject();
        param.put("areaid", AppLoginModel.getAreaId(mContext));
        AppContactsModel.GetMainOrganizationInfo(mContext, param, new BaseListener() {
            @Override
            public void success(String val, String action) {
                binding.ptrClassicFrameLayout.refreshComplete();
                JSONArray array = new JSONArray();
                if (!StringUtil.isNull(val)) {
                    try {
                        array = JSON.parseArray(val);
                    } catch (Exception e) {
                        array.add(JSON.parseObject(val));
                    }
                }
                binding.ptrClassicFrameLayout.setLoadMoreEnable(!(array.size() < pageSize));
                listViewAdapter.addItems(array);
            }

            @Override
            public void error(String error, String action) {
                binding.ptrClassicFrameLayout.refreshComplete();
                showMsg(error);
            }
        });

    }

    private void queryMore() {
        pageNo += 1;
        JSONObject param = new JSONObject();
        param.put("areaid", AppLoginModel.getAreaId(mContext));
        AppContactsModel.GetMainOrganizationInfo(mContext, param, new BaseListener() {
            @Override
            public void success(String val, String action) {
                JSONArray array = new JSONArray();
                try {
                    array = JSON.parseArray(val);
                } catch (Exception e) {
                    array.add(JSON.parseObject(val));
                }
                binding.ptrClassicFrameLayout.loadMoreComplete(!(array.size() < pageSize));
                listViewAdapter.addItems(array);
            }

            @Override
            public void error(String error, String action) {
                binding.ptrClassicFrameLayout.loadMoreComplete(false);
                showMsg(error);
            }
        });
    }
}
